import { CITY_INIT, CITY_UPDATE, CITY_CLEAR } from "../actionTypes"
// 引入异步获取地图
import { getLocalCity, getPoint } from "../../utils/baiduMap"

// 异步获取地理位置
export const actionInitCity = () => {

    return (dispatch) => {
        getLocalCity().then(res => {
            console.log(res);
            res.name = res.name.replace('市', '');
            dispatch({
                type: CITY_INIT,
                value: res
            })
        })
    }
}
/**
 * @param {String} cityname 城市名称
 */
export const actionUpdateCity = (cityname) => {
    return (dispatch) => {
        return getPoint(cityname).then(res => {
            const action = {
                type: CITY_UPDATE,
                value: {
                    name: cityname,
                    center: res
                }
            }
            dispatch(action);
        })
    }
}

/**
 * 清除之前旧的城市名称
 *
 */
export const actionClearCity = () => {
    return {
        type: CITY_CLEAR,
        value: {}
    }
}